package com.tbit.uqbike.service.rpcservice.impl;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.constant.MachineFunctionConstant;
import com.tbit.uqbike.constant.RedisConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.business.CalibrateUtils;
import com.tbit.uqbike.object.business.TerLastStatus;
import com.tbit.uqbike.object.pojo.*;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.MachineInfo;
import com.tbit.uqbike.object.view.MachineShowStatus;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.base.RedisService1;
import com.tbit.uqbike.service.business.MachineFunctionService;
import com.tbit.uqbike.service.rpcservice.MachineStatusService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import com.tbit.uqbike.webmanager.dao.core.*;
import com.tbit.uqbike.webmanager.util.IntegerUtil;
import com.tbit.uqbike.webmanager.util.RedisUtil;
import com.tbit.uqbike.webmanager.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("machineStatusService")
public class MachineStatusServiceImpl implements MachineStatusService {
    @Autowired
    private MachineStatusDao machineStatusDao;
    @Autowired
    private MachineDao machineMapper;
    @Autowired
    private PurviewService purviewService;
    @Autowired
    private RideLogDao rideLogDao;
    @Autowired
    private ParkPointDao parkPointMapper;
    @Autowired
    private MachineBorrowDao machineBorrowDao;
    @Autowired
    private ParkDao parkDao;
    @Autowired
    private MachineLastUseDao machineLastUseDao;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private RedisService1 redisService1;
    @Autowired
    private MachineFunctionService machineFunctionService;

    @Override
    public MachineStatus getByMachineId(Integer machineId) {
        return machineStatusDao.getByMachineId(machineId);
    }

    @Override
    public MachineStatus getByMachineNO(String machineNO) {
        return machineStatusDao.getByMachineNO(machineNO);
    }

    @Override
    public List<MachineStatus> getByMachineIdBatch(List<Integer> machineIds) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineIds", StringUtil.getListString(machineIds));

        return machineStatusDao.getByMachineIdBatch(params);
    }

    @Override
    public List<MachineStatus> getByAccountId(Integer accountId) {
        return machineStatusDao.getByAccountId(accountId);
    }

    @Override
    public List<MachineStatus> getSocByAccountId(Integer accountId, Integer socPercent) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        params.put("socPercent", socPercent);

        return machineStatusDao.getSocByAccountId(params);
    }

    @Override
    public List<MachineStatusFlag> getUnUseByAccountId(Integer accountId, String lastBorrowTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        params.put("lastBorrowTime", lastBorrowTime);

        return machineStatusDao.getUnUseByAccountId(params);
    }

    @Override
    public List<MachineStatus> getFaultByAccountId(Integer accountId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);

        return machineStatusDao.getFaultByAccountId(params);
    }

    @Override
    public MachineInfo getMachineInfo(LoginInfo info, Integer machineId, Integer mapType) {
        MachineInfo machineInfo = new MachineInfo();
        /**查询设备是否存在*/
        Machine machine = machineMapper.selectByPrimaryKey(machineId);
        if (machine == null) {
            throw new BaseException(SystemCodeEnum.MACHINE_NOT_EXIST);
        }
        if (!purviewService.purviewCheck(info.id, machine)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        machineInfo.setAccountId(machine.getAccountId());
        /**查询最后状态*/
        Map<String, Object> map = new HashMap<>();
        map.put("machineId", machineId);
        map.put("sortByBatDt", false);
        List<MachineStatusFlag> machineStatusFlags = machineStatusDao.getMachineStatusFlagByAccId(map);

        MachineStatusFlag status = null;
        if (machineStatusFlags != null && !machineStatusFlags.isEmpty()) {
            status = machineStatusFlags.get(0);
            /**获取实时信号状态*/
            String json = redisService1.get(RedisUtil.getTerHashId(machine.getMachineNO()), RedisConstant.REDIS_TER_LAST_STATUS);
            if (json != null && json.length() > 0) {
                TerLastStatus terLastStatus = JSON.parseObject(json, TerLastStatus.class);
                status.setTerStatus(terLastStatus.getCarStatus());
            }
            CalibrateUtils.calibrateObject(calibrateService, status, mapType);
        }
        machineInfo.setMachineStatus(status);

        /**查询借出信息*/
        RideLogEx rideLog = rideLogDao.findLastByMachineId(machineId);
        machineInfo.setRideLog(rideLog);

        /**查询归属站点信息*/
        ParkPoint parkPoint = parkPointMapper.getByMachineId(machineId);
        machineInfo.setParkPoint(parkPoint);

        // 查询最后扫码人
        MachineLastUse machineLastUse = machineLastUseDao.getByMachineId(machineId);
        if (null != machineLastUse) {
            CalibrateUtils.calibrateObject(calibrateService, machineLastUse, mapType, "lo", "loC", "la", "laC");
        }
        machineInfo.setMachineLastUse(machineLastUse);

        MachineShowStatus machineShowStatus = new MachineShowStatus();
        machineShowStatus.setMachineBusState(1);
        machineShowStatus.setMachineMainState(machine.getState());
        machineShowStatus.setMachineLockState(1);
        machineShowStatus.setMachineParkState(status.getParkState());

        /**查询车辆当前租车信息*/
        MachineBorrow machineBorrow = machineBorrowDao.getByMachineId(machineId);
        if (machineBorrow != null) {
            machineShowStatus.setMachineBusState(2);
            /**查询车辆临停信息*/
            Park park = parkDao.getParkByMachineId(machineId);
            if (park != null) {
                machineShowStatus.setMachineBusState(3);
            }
        }
        if (status != null) {
            Boolean lock = IntegerUtil.getLockState(status.getTerStatus());
            machineShowStatus.setMachineLockState(lock ? 1 : 0);
        }
        machineInfo.setMachineShowStatus(machineShowStatus);
        /***/
        MachineFunction machineFunction = machineFunctionService.getByMachineId(machineId,
                MachineFunctionConstant.MACHINE_TYPE_RULE_RIDE);
        if (machineFunction != null) {
            machineInfo.setMachineFunctionType(1);
        } else {
            machineInfo.setMachineFunctionType(0);
        }
        return machineInfo;
    }

}  